using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using OpenComposite.EII.Repository;

namespace OpenComposite.EII.Designer.Org.Forms
{
	/// <summary>
	/// Summary description for frmNewOrganization.
	/// </summary>
	public partial class FormNewOrganization : Form
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="FormNewOrganization"/> class.
		/// </summary>
		/// <param name="da">The da.</param>
		public FormNewOrganization()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.DialogResult = DialogResult.Cancel;
		}

		/// <summary>
		/// Gets or sets the admin user ID.
		/// </summary>
		/// <value>The admin user ID.</value>
		public string AdminUserId { get; set; }
		/// <summary>
		/// Gets or sets the admin password.
		/// </summary>
		/// <value>The admin password.</value>
		public string AdminPassword { get; set; }
		/// <summary>
		/// Gets or sets the admin orgname.
		/// </summary>
		/// <value>The admin orgname.</value>
		public string AdminOrgname { get; set; }

		private bool testAndSaveData()
		{
			if ( string.IsNullOrEmpty(txtNewOrgName.Text) ) {
				MessageBox.Show("You must enter a organization name.");
				txtNewOrgName.Focus();
				return false;
			}
			if ( string.IsNullOrEmpty(txtAdminFirstName.Text) ) {
				MessageBox.Show("You must enter the first name of the administrator.");
				txtAdminFirstName.Focus();
				return false;
			}
			if ( string.IsNullOrEmpty(txtAdminLastName.Text) ) {
				MessageBox.Show("You must enter the last name of the administrator.");
				txtAdminLastName.Focus();
				return false;
			}
			if ( string.IsNullOrEmpty(txtAdminUserID.Text) ) {
				MessageBox.Show("You must enter the user name of the administrator.");
				txtAdminUserID.Focus();
				return false;
			}
			if ( txtAdminPassword.Text != txtAdminPassword2.Text ) {
				MessageBox.Show("The password and the repeated password are not the same.");
				txtAdminPassword.Focus();
				txtAdminPassword.SelectAll();
				return false;
			}
			IRepositoryItem parent = null;

			Organization org = Global.Repository.GetNewItem<Organization>(false, ref parent);
			org.Name = txtNewOrgName.Text;
			org.Save();

			parent = null;

			Role roleAdmin = Global.Repository.GetNewItem<Role>(false, ref parent);
			roleAdmin.Name = "Administrator";
			roleAdmin.Organisation = org;
			roleAdmin.Save();
			parent = null;

			User user = Global.Repository.GetNewItem<User>(false, ref parent);
			user.BeginEdit();
			user.UserName = txtAdminUserID.Text;
			user.Password = txtAdminPassword.Text;
			user.FirstName = txtAdminFirstName.Text;
			user.LastName = txtAdminLastName.Text;

			user.Address = txtAddress.Text;
			user.Company = txtCompany.Text;
			user.Email = txtEmailAddress.Text;
			user.PhoneBusiness1 = txtPhone.Text;
			user.ZipCode = txtZIP.Text;
			user.City = txtCity.Text;
			user.State = txtState.Text;
			user.Country = txtCountry.Text;
			user.IsSystemAdmin = true;

			user.Organization = org;
			user.EndEdit(true);

			user.AddRole(roleAdmin);

			user.Save();

			AdminUserId = txtAdminUserID.Text;
			AdminPassword = txtAdminPassword.Text;
			AdminOrgname = txtNewOrgName.Text;

			return true;
		}

		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			if ( testAndSaveData() ) {
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
		}

		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
	}
}
